<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	public function index(){
		$data['main_view']='admin/main';
		$this->load->view('login',$data);
	}
	
	
	function __construct() {
		parent::__construct();	
		$this->load->model('login_model');
		$this->load->model('master/master_lembaga');
	}

	function doLogin(){
		$username   = $this->input->post('username');
		$password   = $this->input->post('password');
		
		$pass   = md5($password);
		$cek=$this->login_model->doLogin($username,$pass);
		if($cek!=false){
			if($this->login_model->cekStatus($cek['id_user'])){
				$detail_lembaga=$this->master_lembaga->detail($cek['id_lembaga']);
				$data = array (
				'isLogin' => 'yes',
				'id'=>$cek['id_user'],
				'user_id' => $cek['username'],
				'id_lembaga' => $cek['id_lembaga'],
				'nama_lembaga' => $detail_lembaga['nama_lembaga'],
				'nama' => $cek['nama'],
				);
				$this->session->set_userdata($data);
				redirect('dashboard');
			}else{
				$this->session->set_flashdata('error_login', 'Username ini sudah tidak Aktif !');
				redirect('login');
			}
		}else{
			$this->session->set_flashdata('error_login', 'Username atau password anda salah !');
			redirect('login');
		}
	}
	
	function logOut(){
		$this->session->unset_userdata('isLogin');
		$this->session->unset_userdata('user_id');
		$this->session->sess_destroy();
		$this->session->set_flashdata('error_login', 'Anda Sudah Log Out!');
		redirect('login');
	}	
}